<script>
import { Setting, Fold, Bell, ArrowDown } from "@element-plus/icons-vue";
import TreeMenu from "../components/TreeMenu.vue";
import BreadCrumb from "../components/BreadCrumb.vue";
export default {
  name: "home",
  components: {
    setting: Setting,
    fold: Fold,
    bell: Bell,
    ArrowDown,
    TreeMenu,
    BreadCrumb,
  },
  data() {
    return {
      userInfo: {
        userName: "王敏敏",
        userEmali: "1484605747@qq.com",
      },
      isCollapse: false,
      leaveCount: 0,
      userMenu: null,
    };
  },
  mounted() {
    this.getLeaveCount();
    this.getMenuList();
  },
  methods: {
    handleLogout(e) {
      if (e == "email") return;
      this.$store.commit("saveUserInfo", "");
      this.userInfo = {};
      this.$router.push("/login");
    },

    foldClick() {
      this.isCollapse = !this.isCollapse;
    },

    getLeaveCount() {
      this.$api.noticeCount().then((res) => {
        this.leaveCount = res;
      });
    },

    async getMenuList() {
      let {menuList,actionList} = await this.$api.getPermissionList();
      this.userMenu = menuList;
      this.$store.commit("saveMenuList",menuList)
      this.$store.commit("saveActionList",actionList)
    },
  },
};
</script>

<template>
  <div>
    <div class="home">
      <div :class="['home-sidemenu', isCollapse ? 'fold' : 'unfold']">
        <div class="home-logo">
          <img class="home-img" src="../assets/logo.png" alt="" />
          <span>Manager</span>
        </div>

        <el-menu
          class="home-el-menu"
          active-text-color="#ffd04b"
          text-color="#fff"
          background-color="#040e25"
          :collapse="isCollapse"
          router
        >
          <tree-menu :userMenu="userMenu"></tree-menu>
        </el-menu>
      </div>
      <div :class="['home-right', isCollapse ? 'fold' : 'unfold']">
        <div class="home-topbar">
          <div class="home-bread-box">
            <fold class="home-icon-fold" @click="foldClick"></fold>
            <div class="home-bread">
              <bread-crumb></bread-crumb>
            </div>
          </div>
          <div>
            <el-badge :is-dot="leaveCount > 0" class="home-info-box">
              <el-icon>
                <bell></bell>
              </el-icon>
            </el-badge>
            <el-dropdown @command="handleLogout">
              <span class="el-dropdown-link" style="cursor: pointer">
                {{ userInfo.userName }}
                <el-icon class="home-arrow el-icon--right">
                  <arrow-down />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="email"
                    >邮箱：{{ userInfo.userEmali }}</el-dropdown-item
                  >
                  <el-dropdown-item command="logout">退出</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
        <div class="home-content">
          <!-- <div class="home-mainpage"> -->
          <router-view></router-view>
          <!-- </div> -->
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.home {
  position: relative;
  .home-sidemenu {
    position: fixed;
    width: 200px;
    height: 100vh;
    background-color: rgb(4, 14, 37);
    // overflow-y: auto;
    transition: width 0.5s;
    .home-logo {
      display: flex;
      align-items: center;
      font-size: 18px;
      height: 50px;
      color: #fff;

      img {
        margin: 0 15px;
        width: 32px;
        height: 32px;
      }
    }
    .home-el-menu {
      border-right: none;
      height: calc(100vh - 50px);
      .home-icon-setting {
        width: 20px;
        margin-right: 10px;
      }
    }
    &.fold {
      width: 64px;
    }
    &.unfold {
      width: 200px;
    }
  }

  .home-right {
    margin-left: 200px;
    .home-topbar {
      height: 50px;
      line-height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px;
      border-bottom: 1px solid #ccc;
      .home-bread-box {
        display: flex;
        z-index: 99;
        // justify-content: center;
        align-items: center;
        .home-icon-fold {
          width: 20px;
          height: 20px;
          margin-right: 15px;
        }
        .home-bread {
          line-height: 50px;
        }
      }
      .home-info-box {
        line-height: 30px;
      }
      .el-dropdown-link {
        cursor: pointer;
        color: #4091ff;
        .home-arrow {
          width: 25px;
        }
      }
    }

    .home-content {
      background-color: #f0f0f0;
      padding: 20px;
      height: calc(100vh - 50px);
      .home-mainpage {
        height: 100%;
        width: 100%;
        background-color: #ffffff;
      }
    }

    &.fold {
      margin-left: 64px;
    }
    &.unfold {
      margin-left: 200px;
    }
  }
}
</style>
